package com.bo.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.bo.Entity.Company;
import com.bo.Service.CompanyService;


@Component
public class UserValidator implements Validator{
	
	
	@Autowired
	private CompanyService personService;
	
	
	public boolean supports(Class clazz) {
		  return Company.class.isAssignableFrom(clazz);
	}
	
	
	public void validate(Object obj, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "account", "required", "Field is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required", "Field is required");
	
	}
	
	
	
	public void validateAccount(String account, Errors errors){
		
		if(personService.checkAccount(account)){

			errors.rejectValue("password", "validation.error", "password error");
		
		}else{
			errors.rejectValue("account", "validation.not_exists", "account is not exist");
		}
	}

}
